<?php

// +----------------------------------------------------------------------
// | MyCentOS
// +----------------------------------------------------------------------
// | Copyright (c) 2014 http://www.mycentos.com, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 琥珀 <317559272@qqq.com><http://www.mycentos.com>
// +----------------------------------------------------------------------
// | @date  : 2015-03-05 14:13:54
// +----------------------------------------------------------------------
// | @Last Modified by:  琥珀
// +----------------------------------------------------------------------
// | @Last Modified time: 2015-03-14 17:36:45
// +-----------------------------------------------------------------------


/**
 * 科室控制器
 * @authors 琥珀 (317559272@qq.com)
 */
namespace Admin\Controller;
use Think\Controller;
Class DepartmentController extends AdminController
{
    public function index() {
        $model = M('Department');
        $searchname = I('searchname');
        if ($searchname) {
            $map['id|name|author'] = array(intval($searchname), array('like', '%' . $searchname . '%'), array('like', '%' . $searchname . '%'), '_multi' => true);
        }
        $list = $this->lists($model, $map);
        foreach ($list as $key => $value) {
            $hospital_name = get_hospital_name($value['hospital_id']);
            $list[$key]['hospital_name'] = $hospital_name ? $hospital_name : null;
        }
        $this->assign('list', $list);
        $this->assign('searchname', $searchname);
        $this->display();
    }
    public function add() {
        if (IS_POST) {
            $Department = M('Department');
            $data = I('post.');
            $data['author'] = get_user_name();
            $data['addtime'] = time();
            $data['lasttime'] = time();
            if (false === $Department->create($data)) {
                $this->error($Department->getError());
            }
            $list = $Department->add();
            if ($list !== false) {
                $this->success('新增成功!');
            } else {
                $this->error('新增失败!');
            }
        } else {

            //查询医院
            $Hospital = M('Hospital');
            $data = $Hospital->select();
            $this->assign('hospital', $data);
            $this->display();
        }
    }
    public function edit() {
        if (IS_POST) {
            $Department = M('Department');
            $data = I('post.');
            $data['author'] = get_user_name();
            $data['lasttime'] = time();
            $Department->create($data);
            $result = $Department->save();
            if ($result) {
                $this->success('修改成功');
            } else {
                $this->error('修改失败');
            }
        } else {
            $id = I('id');
            if (empty($id)) {
                $this->error('请选择要操作的数据!');
            }
            $map = array('id' => $id);
            $Department = M("Department");
            $data = $Department->where($map)->find();
            $hospitals = M('Hospital')->select();
            $this->assign('hospitals', $hospitals);
            $this->assign('data', $data);
            $this->display();
        }
    }
    public function del($ids = null) {
        empty($ids) && $this->error('参数错误');
        $ids = is_array($ids) ? implode(',', $ids) : $ids;
        $map['id'] = array('in', $ids);
        $res = M('Department')->where($map)->delete();
        if ($res !== false) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }
}
?>